import os
from typing import Optional


import openai
from openai import OpenAI


# from tool import get_completion
from dotenv import load_dotenv, find_dotenv


def get_openai_key():  
    print("dotenv: ",find_dotenv())
    _ = load_dotenv(find_dotenv())
    return OpenAI()


    # api_key = os.environ.get('OPENAI_API_KEY')
    # if api_key:
    #     openai.api_key = api_key
    # else:
    #     raise ValueError("API KEY NOT FOUND")
    # return OpenAI(openai.api_key)


client = get_openai_key()


# Set the http_proxy and https_proxy environment variables
# os.environ['http_proxy'] = 'http://127.0.0.1:7890'
# os.environ['https_proxy'] = 'http://127.0.0.1:7890'






def get_completion(prompt, model="gpt-4",temperature = 0):
    messages = [{"role": "user", "content": prompt}]
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {
              "role": "system",
              "content": "You will be provided with unstructured data, and your task is to parse it into CSV format."
            },
            {
              "role": "user",
              "content": "There are many fruits that were found on the recently discovered planet Goocrux. There are neoskizzles that grow there, which are purple and taste like candy. There are also loheckles, which are a grayish blue fruit and are very tart, a little bit like a lemon. Pounits are a bright green color and are more savory than sweet. There are also plenty of loopnovas which are a neon pink flavor and taste like cotton candy. Finally, there are fruits called glowls, which have a very sour and bitter taste which is acidic and caustic, and a pale orange tinge to them."
            }
        ],
        temperature=temperature
    )
    return response.choices[0].message


text=f"""
您应该提供尽可能清晰、具体的指示，以表达您希望模型执行的任务。\
这将引导模型朝向所需的输出，并降低收到无关或不正确响应的可能性。\
不要将写清晰的提示词与写简短的提示词混淆。\
在许多情况下，更长的提示词可以为模型提供更多的清晰度和上下文信息，从而导致更详细和相关的输出。
"""
# 需要总结的内容
prompt =f"""
把用三个反引号括起来的文本总结成一句话。
```{text}```
"""
# 指令内容，使用 ``` 来分隔指令和待总结的内容
response = get_completion(prompt)
print(response)

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
